package dp.back01;
//416. 分割等和子集

/**
 * 01背包
 */
public class Num416_canPartition {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        int []dp = new int[20001];
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        if (sum % 2 == 1)return false;
        int target = sum / 2;

        //开始01背包
        for (int i = 0; i < nums.length; i++) {
            for (int j = target; j >= nums[i] ; j--) {
                dp[j] = Math.max(dp[j] , dp[j - nums[i]] + nums[i]);
            }
        }
        if (dp[target] == target)return true;
        return false;
    }
}
